package com.xul.crowd.controller;

import com.xul.crowd.constant.CrowdConstant;
import com.xul.crowd.service.MemberService;
import com.xul.crowd.util.ResultEntity;
import com.xul.entity.po.MemberPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

/**
 * @author xul
 * @create 2021-07-29 15:17
 */
@RestController
public class MemberProviderController {

    @Autowired
    private MemberService memberService;

    /**
     * 保存会员信息到数据库 给auth远程调用
     *
     * @param memberPO
     * @return
     */
    @RequestMapping("save/member/remote")
    public ResultEntity<String> saveMember(@RequestBody MemberPO memberPO) {
        try {
            memberService.saveMember(memberPO);
            return ResultEntity.successWithData();
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return ResultEntity.failed(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
            }
            return ResultEntity.failed(e.getMessage());
        }
    }

    /**
     * 根据会员的账号 查询账号信息 给auth远程调用
     *
     * @param loginAcct
     * @return
     */
    @RequestMapping("get/memberPo/by/login/acct/remote")
    public ResultEntity<MemberPO> getMemberPOByLoginAcctRemote(String loginAcct) {

        try {
            MemberPO memberPO = memberService.getMemberPOBByLoginAcct(loginAcct);
            return ResultEntity.successWithData(memberPO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }
}
